// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// e [author] rhyme_lph
// c [email]  rhymelph@gmail.com
// P [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/http_service/http_login_service.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/widgets/bottom_sheet/default_bottom_sheet.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:flutter/material.dart';

class SetNewPasswordProvider extends BaseProvider {
  TextEditingController passwordController = TextEditingController();
  FocusNode passwordFocusNode = FocusNode();

  bool enable = false;

  bool passwordError = false;

  String phoneNumber;

  String email;

  String otp;

  String countryCode;

  @override
  void dispose() {
    super.dispose();
    passwordController.dispose();
    passwordFocusNode.dispose();
  }

  void onUpdatePassword() async {
    passwordFocusNode.unfocus();
    String password = passwordController.text;
    if (password.length < 6) {
      passwordError = true;
      update();
      return;
    }
    bool isSuccess = false;
    if (email != null) {
      await post(HttpLoginService.resetEmail(email, password, otp),
          autoHandler: AutoHandler.Toast,
          loadingTip: S.current.loading, onSuccess: (result) {
        isSuccess = true;
      });
    } else {
      await post(
          HttpLoginService.resetPhoneNumber(
              countryCode, phoneNumber, password, otp),
          autoHandler: AutoHandler.Toast,
          loadingTip: S.current.loading, onSuccess: (result) {
        isSuccess = true;
      });
    }

    if (isSuccess == true)
      obtainContext?.call((context) async {
        await showDefaultBottomSheet(context,
            child: Column(
              children: [
                Text(
                  S.current.your_password_has_been_changed_successfully,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: aceBlackColor,
                    fontSize: getSp(22),
                    fontWeight: FontWeight.w700,
                    height: 24 / 22,
                  ),
                ),
                SizedBox(
                  height: getDp(29),
                ),
                NormalButton(
                  onTap: () => Navigator.of(context).pop(),
                  title: S.current.got_it,
                ),
              ],
            ));
        Routes.navigateTo(RouterGenProviders.loginPage, clearStack: true);
      });
  }

  @override
  void initState() {
    super.initState();
    passwordController.addListener(_onUpdateInput);
  }

  void _onUpdateInput() {
    if (passwordController.text.length > 0) {
      if (enable == false) {
        enable = true;
        update();
      }
    } else {
      if (enable == true) {
        enable = false;
        update();
      }
    }
  }

  @override
  Future get dataFuture => null;
}
